package com.wangsd.system.controller;

import com.gitee.fastmybatis.core.PageInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wangsd.common.controller.AjaxResult;
import com.wangsd.common.controller.BaseController;
import com.wangsd.common.jwt.LoginUser;
import com.wangsd.common.jwt.TokenService;
import com.wangsd.common.utils.ServletUtils;
import com.wangsd.system.entity.Trade;
import com.wangsd.system.service.TradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单Controller
 *
 * @author wsd
 * @date 2021-06-25
 */
@Api(tags = "订单管理")
@RestController
@RequestMapping("/system/trade")
public class TradeController extends BaseController {
    @Autowired
    private TradeService tradeService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation(value = "查询订单列表")
    @GetMapping("/list")
    public AjaxResult<PageInfo<Trade>> list(Trade trade) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());

        Page page = PageHelper.startPage(ServletUtils.getParameterToInt("pageNum"), ServletUtils.getParameterToInt("pageSize"));
        List<Trade> list = tradeService.selectTradeList(trade);
        return AjaxResult.success(getPageInfo(page, list));
    }


    @ApiOperation(value = "获取订单详细信息")
    @GetMapping("/{id}")
    public AjaxResult<Trade> getInfo(@PathVariable("id") Long id) {
        Trade trade = tradeService.selectTradeById(id);
        return AjaxResult.success(trade);
    }

    @ApiOperation(value = "新增/修改订单")
    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody Trade trade) {
        if (trade.getId() == null){
            tradeService.insertTrade(trade);
        } else {
            tradeService.updateTrade(trade);
        }
        return AjaxResult.success("success");
    }


    @ApiOperation(value = "删除订单")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        tradeService.deleteTradeById(id);
        return AjaxResult.success("success");
    }
}
